package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserIdThreadLocal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.aspectj.util.UtilClassLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;


    //清空购物车
    @Override
    public R cleanShoppingCart() {
        Long userId = UserIdThreadLocal.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartMapper.delete(qw);
        return R.success("清空购物车成功");
    }
    //修改购物车

    @Override
    public R subShoppingCart(ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = UserIdThreadLocal.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        //判断是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            //添加菜品id
            Long dishId = shoppingCart.getDishId();
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加套餐id
            Long setmealId = shoppingCart.getSetmealId();
            qw.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //获取对象
        ShoppingCart oneShoppingCart = shoppingCartMapper.selectOne(qw);
        //获取数量
        Integer number = oneShoppingCart.getNumber();
        //判断是否为1，为一删除，不为1减1
        if (number > 1) {
            number = number - 1;
            oneShoppingCart.setNumber(number);
            shoppingCartMapper.updateById(oneShoppingCart);
        } else {
            Long shoppingCartId = oneShoppingCart.getId();
            oneShoppingCart.setNumber(0);
            shoppingCartMapper.deleteById(shoppingCartId);
        }
        return R.success(oneShoppingCart);
    }

    //添加购物车
    @Override
    public R addShoppingCart(ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = UserIdThreadLocal.getId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        //判断添加是套餐还是菜品
        if (shoppingCart.getDishId() != null) {
            Long dishId = shoppingCart.getDishId();
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            Long setmealId = shoppingCart.getSetmealId();
            qw.eq(ShoppingCart::getSetmealId, setmealId);
        }
        //获取对象
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(qw);
        //判断数量
        if (shoppingCart1 != null) {
            //不为一，加1
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart1);
            return R.success(shoppingCart1);
        } else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
            return R.success(shoppingCart);
        }

    }


    //查询购物车

    @Override
    public R getAllShoppingCart() {
        //获取用户id
        Long userId = UserIdThreadLocal.getId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(qw);
        return R.success(shoppingCarts);
    }
}
